using System;

namespace KukaDataComCommon
{
    [Serializable]
   public class AxisPosition
   {
      public AxisPosition()
      {
         
      }
      public AxisPosition(float A1, float A2, float A3, float A4, float A5, float A6)
      {
         this.A1 = A1;
         this.A2 = A2;
         this.A3 = A3;
         this.A4 = A4;
         this.A5 = A5;
         this.A6 = A6;
      }

      public float A1 { get; set; }
      public float A2 { get; set; }
      public float A3 { get; set; }
      public float A4 { get; set; }
      public float A5 { get; set; }
      public float A6 { get; set; }

      public override bool Equals(object obj)
      {
         if (obj == null)
            return false;
         if (obj.GetType() != typeof(AxisPosition))
            return false;
         var objX = (AxisPosition)obj;

         return A1 == objX.A1
                && A2 == objX.A2
                && A3 == objX.A3
                && A4 == objX.A4
                && A5 == objX.A5
                && A6 == objX.A6;
      }

      public static bool tryParse(byte[] data, out AxisPosition result)
      {
         result = null;
         if (data != null && data.Length == (6 * 4))
         {
            result = new AxisPosition(BitConverter.ToSingle(data, (0 * 4)),
                                      BitConverter.ToSingle(data, (1 * 4)),
                                      BitConverter.ToSingle(data, (2 * 4)),
                                      BitConverter.ToSingle(data, (3 * 4)),
                                      BitConverter.ToSingle(data, (4 * 4)),
                                      BitConverter.ToSingle(data, (5 * 4)));
            return true;
         }
         return false;
      }

      public override string ToString()
      {
         return string.Format("A1: {0} A2: {1} A3: {2} A4: {3} A5: {4} A6: {5}", A1, A2, A3, A4, A5, A6);
      }
      public static float operator %(AxisPosition a, AxisPosition b)
      {
         return (float)Math.Sqrt((a.A1 - b.A1) * (a.A1 - b.A1) +
                                  (a.A2 - b.A2) * (a.A2 - b.A2) +
                                  (a.A3 - b.A3) * (a.A3 - b.A3) +
                                  (a.A4 - b.A4) * (a.A4 - b.A4) +
                                  (a.A5 - b.A5) * (a.A5 - b.A5) +
                                  (a.A6 - b.A6) * (a.A6 - b.A6));
      }
   }
   [Serializable]
   public class Position
   {

      #region Eigenschaften

      private readonly Location location;
      public Location Location
      {
         get { return location; }
      }

      private readonly Orientation orientation;
      public Orientation Orientation
      {
         get { return orientation; }
      }

      private readonly AxialUniqueness axialUniqueness;
      public AxialUniqueness AxialUniqueness
      {
         get { return axialUniqueness; }
      }

      #endregion

      #region Konstruktor

      public static bool tryParse(byte[] data, out Position result)
      {
         result = null;
         if (data != null && data.Length == (6 * 4 + 2 * 2))
         {
            result = new Position(new Location(BitConverter.ToSingle(data, (0 * 4)),
                                               BitConverter.ToSingle(data, (1 * 4)),
                                               BitConverter.ToSingle(data, (2 * 4))),
                                  new Orientation(BitConverter.ToSingle(data, (3 * 4)),
                                                  BitConverter.ToSingle(data, (4 * 4)),
                                                  BitConverter.ToSingle(data, (5 * 4))),
                                  new AxialUniqueness(BitConverter.ToInt16(data, (6 * 4)),
                                                      BitConverter.ToInt16(data, 6 * 4 + 2)));
            return true;
         }
         return false;
      }

      public Position()
         : this(new Location())
      { }

      public Position(Location location)
         : this(location, new Orientation())
      { }

      public Position(Location location, Orientation orientation)
         : this(location, orientation, new AxialUniqueness())
      { }

      public Position(Location location, Orientation orientation, AxialUniqueness axialUniqueness)
      {
         if (location == null) throw new ArgumentNullException("location");
         if (orientation == null) throw new ArgumentNullException("orientation");
         if (axialUniqueness == null) throw new ArgumentNullException("axialUniqueness");

         this.location = location;
         this.orientation = orientation;
         this.axialUniqueness = axialUniqueness;
      }

      public Position(Position copy)
         : this(copy.Location, copy.Orientation, copy.AxialUniqueness)
      { }

      public Position Copy()
      {
         return new Position(this);
      }

      #endregion

      #region overrides

      public override string ToString()
      {
         return Location + " " + Orientation + " " + AxialUniqueness;
      }

      #endregion

   }
}
